'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2012 Jens Frenkel
'''

import os
import modelimark

# get compiler phat
OPENMODELICAHOME = os.environ.get('OPENMODELICAHOME')
omcpath = '%s/bin/omc.exe'%(OPENMODELICAHOME)

print omcpath

task = 'fromsource_omc'
print task

# workdir
workdir = '%s/%s'%(modelimark.settings.globalworkdir,task)


# generate a source file, this is only for the example,
# normaly you should use a allready existing file

filename = '%s/fromsourceexample.mo'%workdir
modelname = 'fromsource'

if not os.access(workdir, os.F_OK):
    os.mkdir(workdir)

f = open( filename, 'w' )
f.write('model %s\n'%(modelname))
f.write('  input Real inp;\n')

for i in xrange(10):
    f.write('  Real v_%s;\n'%(str(i)))

f.write('equation\n')
for i in xrange(10-1):
    f.write('\tv_%s = %s + v_%s;\n'%(i, i, i+1))

f.write('\tder(v_%s) = v_0 + inp;\n'%(10-1))

f.write('end %s;\n'%(modelname))
f.close()

models = [(modelname,[filename])]

# model
model = modelimark.FromSource()
compiler = modelimark.OMC(omcpath)
bench = modelimark.BenchmarkComponent()
bench.runBenchmark(compiler, model, workdir, '%s.csv'%(task), models)
